﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBUtility;
using MySql.Data.MySqlClient;

namespace OracleDAL.Common
{
    /// <summary>
    /// 数据访问类mysqldata。
    /// 对MySQL基本操作
    /// 只对MySQL数据库进行查询，可以是SQL语句或存储过程
    /// </summary>
    public class mysqldata
    {
        public mysqldata()
        { }
        #region  成员方法
        /// <summary>
        /// 调用存储过程
        /// </summary>
        /// <param name="PRODUCENAME">存储过程名称</param>
        /// <param name="PARAM">参数数组(参数名称、参数类型)</param>
        /// <param name="VALUES">值数组</param>
        /// <returns></returns>
        public DataSet GetList(string PRODUCENAME, string[,] PARAM, string[] VALUES)
        {
            if (PARAM != null)
            {
                /*
                *   Rank为取得数组的秩（维数），可以理解为一个矩阵的行数
                *   GetUpperBound(0) 返回 Array 的第一维的索引上限，GetUpperBound(Rank-1) 返回 Array 的最后一维的上限，也就是列数-1
                */
                int length = PARAM.GetUpperBound(0) + 1;
                MySqlParameter[] param = new MySqlParameter[length];
                for (int i = 0; i < length; i++)
                {
                    switch (PARAM[i, 1].ToString().ToLower())
                    {
                        case "int":
                            param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.Int32);
                            param[i].Value = Convert.ToInt32(VALUES[i]);
                            break;
                        case "date":
                            param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.Date);
                            param[i].Value = Convert.ToDateTime(VALUES[i]);
                            break;
                        case "datetime":
                            param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.DateTime);
                            param[i].Value = Convert.ToDateTime(VALUES[i]);
                            break;
                        case "varchar":
                            param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.VarChar);
                            param[i].Value = VALUES[i];
                            break;
                        default:
                            param[i] = new MySqlParameter(PARAM[i, 0], MySqlDbType.String);
                            param[i].Value = VALUES[i];
                            break;
                    }

                }
                return MySQLHelper.ExecuteDataset(CommandType.StoredProcedure, PRODUCENAME, param);
            }
            return MySQLHelper.ExecuteDataset(CommandType.StoredProcedure, PRODUCENAME, null);

        }
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="SQLTEXT">SQL语句</param>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public DataSet GetList(string SQLTEXT, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(SQLTEXT);
            if (strWhere != "")
            {
                if (SQLTEXT.IndexOf("where") > 0)//如果原SQL语句中就存在where条件(此处这样判断还是很不严密)
                {
                    strSql.Append(" and " + strWhere);
                }
                else //如果原SQL语句不存在where 语句
                {
                    strSql.Append(" where " + strWhere);
                }
            }
            return MySQLHelper.ExecuteDataset(CommandType.Text, strSql.ToString());
        }
        /// <summary>
        /// 获取重点监视基站列表
        /// </summary>
        /// <param name="USERISN">用户ID</param>
        /// <returns></returns>
        public DataSet GetTBSID(string USERISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct(TBS.NE_ID),TBS.EQUIP_ID");
            strSql.Append(" from TB_STATION TBS,RECORDVIDEO RDV ");
            strSql.Append(" where TBS.BELONGROOM=RDV.ROOMID and RDV.USERISN='" + USERISN + "'");
            strSql.Append(" order by  TBS.NE_ID ");
            return MySQLHelper.ExecuteDataset(CommandType.Text,strSql.ToString());
        }
      /// <summary>
      /// 获取大运会重点监控方案列表
      /// </summary>
      /// <returns></returns>
      /*  public DataSet GetProgramList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PROGRAMNAME,SHORTNAME,ISUSED,RECORDTIME,CREATEUSER,REMARK ");
            strSql.Append(" from PROGRAMCONFIG ");
            strSql.Append(" order by  RECORDTIME DESC ");
            return OracleHelper.Query(strSql.ToString());
        }
        */
        #endregion  成员方法
    }
}

